$(document).ready(function(e){
	var phoneflag;
	mui.plusReady(function() {
		var id = plus.navigator.getCookie( "id");
		$.ajax({
			url:backend_url + "/usot/sum.action",
			type:"POST",
			data:{
				"id":id
			},
			dataType:"json",
			success:function(data){
				var user = data.a;
				// console.log(user);
				$("#uname").html(user.userName);
				$("#uname1").val(user.userName);
				$("#password").val(user.password);
				var imgurl;
				if(user.headPosition != null && user.headPosition.length != 0){
					imgurl = img_url+user.headPosition;
					$("#head-img").attr("src",imgurl);
					$("#head-img1").attr("src",imgurl);
				}
				if(user.phoneNumber != null && user.phoneNumber.length != 0){
					$("#phone").val(user.phoneNumber);
				}
				if(user.name != null && user.name.length != 0){
					$("#truename").val(user.name);
				}
			},
			error:function(er){
				alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
				// window.location.reload();
			}
		});
		// 点击修改
		$("#modify_user").on("tap",function(){
			var phoneflag = checkphone();
			if(phoneflag){
				var uname = $("#uname1").val();
				var phone = $("#phone").val();
				var truename = $("#truename").val();
				var password = $("#password").val();
				$.ajax({
					url:backend_url + "/usot/ru.action",
					type:"POST",
					data:{
						"id":id,
						"userName":uname,
						"phoneNumber":phone,
						"name":truename,
						"password":password,
						"headPosition":imgURL
					},
					dataType:"json",
					success:function(data){
						alert("修改成功！");
						window.location.reload();
					},
					error:function(er){
						alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
						// window.location.reload();
					}
				});
			}
		});
	});	
	// 点击修改资料时，可以改动
	$("#modify_click").on("tap",function(){
		$(".head_div").attr("id","head");//点击修改资料时，使$(".head_div")追加上id#head，此时可以修改头像
		$("#mbox input").removeAttr("disabled");
		$("#mbox input").css("border","1px solid #09a267");
		mui.toast("点击对应的信息开始修改~");
		$("#modify_click").hide();
		$("#modify_user").show();
		// 点击可修改头像
		$("#head").on("tap",function(){
			$("#addhead").click();
		});
	});	
	//	头像--图片回显
	var imgURL;
	$("#addhead").on("change", function(){
		var file = this.files[0];
		imgURL = window.URL.createObjectURL(file);
		$("#head-img1").attr("src",imgURL);
		var form = new FormData();
		//大于1M，进行压缩上传
		if(file.size/1024 > 1025){
			photoCompress(file, {
			quality: 0.2
			}, function(base64Codes){
			//console.log("压缩后：" + base.length / 1024 + " " + base);
				var bl = convertBase64UrlToBlob(base64Codes);
				form.append("file", bl, "file_"+Date.parse(new Date())+".jpg"); // 文件对象
				uploadImg(form);
			});
		}else{ //小于等于1M 原图上传
			form.append("file", file); // 文件对象
			uploadImg(form);
		}
	});
	function uploadImg(form){
		$.ajax({
		type: "POST",
		url:backend_url + "/files/fileUpload.action",
		data:form, 
		cache: false,
		contentType: false,
		processData: false,
		async: false,
		success: function(data) {
			console.log("上传路径为："+data.url);
			imgURL=data.url;
		},error:function(data) {
			alert("上传失败，请检查网络设置或重试！");
			imgURL = "";
		}
		});
	}
	// 检查手机号码格式
	$('#phone').on('blur',function () {
		checkphone();
	});
	function checkphone(){
		if( !( /^1[345789]\d{9}$/.test( $("#phone").val().trim() ) ) ){
			$("#phone").css('border', '1px solid #f00');
			mui.toast("手机号码格式不正确！")
			phoneflag = 0;
			return phoneflag;
		}else{
			phoneflag = 1;
			$("#phone").css('border', '1px solid #09a267');
			return phoneflag;
		}
	}
	// 注销
	$("#logoff").click(function(){
		//退出时删除登录id
		$.ajax({
			url:backend_url + "/usot/dl.action",
			type:"POST",
			dataType:"json",
			success:function(data){
				plus.navigator.removeAllCookie();//删除应用所有Cookie值
				plus.webview.open('before-login.html', 'new', {}, 'slide-in-right', 200);
			},
			error:function(er){
				alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
				// window.location.reload();
			}
		});
	});
});

